<?php
namespace app\admin\service;

use library\tools\Node;
use think\Db;
use think\facade\Request;
use think\facade\Validate;
use app\admin\validate\Increase as IncreaseValidate;
use app\admin\service\CompanyService;
use app\admin\service\PointService;

/**
 * 增员服务
 * Class IncreaseService
 * @package app\admin\service
 */
class IncreaseService
{
	
    /**
     * 根据工资获取所有险种的基数
     * @param int $wage 工资
     * @return array
     */
    public static function allBase($wage)
    {
    	$list = Db::name('system_insurance_base')->field('insurance,min,max')->select();
		$arr = [];
		foreach ($list as $k => $v) {
			if($wage <= $v['min']){
				$arr[$v['insurance']]['personal'] = $v['min'];
			}elseif($wage > $v['min'] && $wage < $v['max']){
				$arr[$v['insurance']]['personal'] = sprintf("%.2f",$wage);
			}else{
				if($v['max'] > 0){
					$arr[$v['insurance']]['personal'] = $v['max'];
				}else{
					$arr[$v['insurance']]['personal'] = sprintf("%.2f",$wage);
				}
			}
			if($wage < $v['min']){
				$arr[$v['insurance']]['company'] = $v['min'];
			}else{
				$arr[$v['insurance']]['company'] = sprintf("%.2f",$wage);
			}
		}
		return $arr;
    }
    
    
    /**
     * 根据工资获取单一险种的基数
     * @param int $insurance 险种
	 * @param int $wage 工资
     * @return array
     */
    public static function oneBase($insurance,$wage)
    {
    	$base = Db::name('system_insurance_base')->where(['insurance'=>$insurance])->field('min,max')->find();
		if($wage <= $base['min']){
			$personal = $base['min'];
		}elseif($wage > $base['min'] && $wage < $base['max']){
			$personal = sprintf("%.2f",$wage);
		}else{
			if($base['max'] > 0){
				$personal = $base['max'];
			}else{
				$personal = sprintf("%.2f",$wage);
			}
		}
		if($wage < $base['min']){
			$company = $base['min'];
		}else{
			$company = sprintf("%.2f",$wage);
		}		
		return ['company'=>$company,'personal'=>$personal];
    }
    
    
    /**
     * 导出增员
     * @param array $id
     * @return array
     */
    public static function export_increase($id)
    {
    	$filename = '增员服务列表';
    	$titles = array(
    		array('code','服务编号'),
            array('name','姓名'),
            array('id_number','身份证号'),
            array('mobile','电话号码'),
            array('company_name','公司名称'),
            array('start_date','服务账期'),
            array('wage','月发工资'),
            array('archives','托管档案'),
            array('yanglao_st','养老起始日期'),
            array('gongshang_st','工伤起始日期'),
            array('shiye_st','失业起始日期'),
            array('yiliao_st','医疗起始日期'),
            array('gongjijin_wage','公积金工资'),
            array('gongjijin_st','公积金起始日期'),         
            array('source','数据来源'),
            array('status','状态'),           
            array('create_at','委托日期'),
            array('csr_id','所属客服'),
            array('desc','备注信息'),
        );
    	$list = Db::name('system_increase')->where(['id'=>$id])->order('id desc')->select();
		if(!empty($list) && $list){
			$companys = Db::name('system_company')->column('name','id');
			foreach($list as $k=>$v){
				$increase_insurance = Db::name('system_increase_insurance')->where(['increase_id'=>$v['id']])->column('start_date','insurance');
	    		$arr[$k]['code'] = $v['code'];
	    		$arr[$k]['name'] = $v['name'];
				$arr[$k]['id_number'] = ' '.$v['id_number'];
				$arr[$k]['mobile'] = $v['mobile'];
				$arr[$k]['company_name'] = $companys[$v['company_id']];
	    		$arr[$k]['start_date'] = $v['start_date'];
				$arr[$k]['wage'] = $v['wage'];
				$arr[$k]['archives'] = $v['archives'] == 1 ? '是' : '否';
				$arr[$k]['yanglao_st'] = $increase_insurance[1];
				$arr[$k]['gongshang_st'] = $increase_insurance[2];
				$arr[$k]['shiye_st'] = $increase_insurance[3];
				$arr[$k]['yiliao_st'] = $increase_insurance[4];
				if($increase_insurance[5]){
					$gongjijin_wage = Db::name('system_increase_insurance')->where(['increase_id'=>$v['id'],'insurance'=>5])->value('wage');
				}else{
					$gongjijin_wage = '';
				}
				$arr[$k]['gongjijin_wage'] = $gongjijin_wage;
				$arr[$k]['gongjijin_st'] = $increase_insurance[5];				
				$arr[$k]['source'] = ['1'=>'后台','2'=>'PC网站','3'=>'移动H5'][$v['source']];
				$arr[$k]['status'] = ['1'=>'待审核','2'=>'已审核','3'=>'已反馈'][$v['status']];
				$arr[$k]['create_at'] = date('Y-m-d',strtotime($v['create_at']));
				$arr[$k]['csr_id'] = $v['csr_id'] ? user_list()[$v['csr_id']] : '';
				$arr[$k]['desc'] = $v['desc'];
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }


	/**
     * 增员确认
     * @param string $increase_id
     * @param string $company_id
     * @param string $contract_id
     * @param string $person_id
     * @param string $insurance
	 * @param string $area
	 * @param string $status
	 * @param string $wage
	 * @param string $company_base
	 * @param string $personal_base
	 * @param string $start_date
	 * @param string $csr_id
	 * @param string $hand_type
	 * @param string $account_period
	 * @param string $desc
     * @return number
     */
    public static function increase_confirm($increase_id,$company_id,$contract_id,$person_id,$insurance,$area,$status,$wage,$company_base,$personal_base,$start_date,$csr_id,$hand_type,$account_period,$desc)
	{
		$arr = [
			'increase_id' => $increase_id,
			'company_id' => $company_id,
			'contract_id' => $contract_id,
			'person_id' => $person_id,
			'insurance' => $insurance,
			'area' => $area,		
			'status' => $status,							
			'wage' => $wage,
			'company_base' => $company_base,
			'personal_base' => $personal_base,			
			'start_date' => $start_date,
			'csr_id' => $csr_id,
			'hand_type' => $hand_type,
			'account_period' => $account_period,
			'desc' => $desc,
		];
		$re = Db::name('system_insurance_log')->insertGetId($arr);
		if($re){
			$count = Db::name('system_insurance_result')->where(['person_id'=>$person_id,'insurance'=>$insurance])->count();
			if($count){
				unset($arr['person_id']);
				unset($arr['insurance']);
				$arr['inslog_id'] = $re;
				Db::name('system_insurance_result')->where(['person_id'=>$person_id,'insurance'=>$insurance])->update($arr);
			}else{
				$arr['inslog_id'] = $re;
				Db::name('system_insurance_result')->insert($arr);
			}
			//修改减少险种表
			Db::name('system_downsizing_insurance')->where(['increase_id'=>$increase_id,'insurance'=>$insurance])->update(['is_deleted'=>1]);				
		} 
	}


	 /**
     * 判断某人某险种是否能确认
     * @param array $person_id
	 * @param array $insurance
     * @return array
     */
    public static function is_confirm($person_id,$insurance)
	{
		$info = Db::name('system_insurance_result')->where(['person_id'=>$person_id,'insurance'=>$insurance])->find();
		if(!$info || $info['status'] != 1){
			return true;
		}else{
			return false;
		}		
	}
	
	
	/**
     * 增减员提交，检测次数或者点数
     * @param array $company_id
     * @return array
     */
    public static function deduction_check($company_id)
	{
		//获取增减员服务id
		$service = Db::name('system_service')->where(['is_special'=>2])->find();
		//找到主公司
		$master = CompanyService::getMaster($company_id);
		$info = Db::name('system_member_count')->where(['member_id'=>$master['mid'],'service_id'=>$service['id'],'is_deleted'=>0])->find();
		if(!$info || $info['surplus'] <= 0){
			//没有次数，查找点数
			if($service['point'] > $master['point']){
				return false;
			}else{
				return 1;
			}
		}else{
			return 2;
		}		
	}
	
	
	 /**
     * 增员提交，扣除费用
     * @param array $id  增员表id
     * @return array
     */
    public static function deduction($id)
	{
		$service = Db::name('system_service')->where(['is_special'=>2])->find();
		$info = Db::name('system_increase')->where(['id'=>$id])->find();
		$contract = Db::name('system_contract')->where(['id'=>$info['contract_id'],'status'=>1])->find();
		if($contract['setmeal_type'] == 1){
			$company = Db::name('system_company')->where(['id'=>$info['company_id']])->find();
			//找到主公司
			$master = CompanyService::getMaster($info['company_id']);
			$desc = $info['code'].'-'.$service['title'];		
			if($info['deduction'] == 1){
				//扣除服务点			
				$re = PointService::pointDetAdd($master['mid'],$company['mid'],$info['contract_id'],2,$service['point'],$desc,$master['unique']);
				if($re){
					Db::name('system_company')->where(['mid'=>$master['mid']])->setDec('point', $service['point']);
				}
			}else{
				//扣除服务次数
				$re = PointService::frequencyDetAdd($master['mid'],$company['mid'],$info['contract_id'],$service['id'],2,1,$desc,$master['unique']);
				if($re){
					Db::name('system_member_count')->where(['member_id'=>$master['mid'],'service_id'=>$service['id']])->setDec('surplus');
					Db::name('system_member_count')->where(['member_id'=>$master['mid'],'service_id'=>$service['id']])->setInc('used');
				}
			}
		}	
	}
	
	
	 /**
     * 增员待审核删除，返还扣除费用
     * @param array $ids
     * @return array
     */
    public static function deduction_back($ids)
	{
		$service = Db::name('system_service')->where(['is_special'=>2])->find();
		$list = Db::name('system_increase')->where(['id'=>$ids])->field('id,code,company_id,contract_id,deduction,point')->select();
		if($list){
			foreach($list as $k=>$v){
				//找到主公司
				$master = CompanyService::getMaster($v['company_id']);
				$company = Db::name('system_company')->where(['id'=>$v['company_id']])->find();
				$desc = $v['code'].'-'.$service['title'];	
				if($v['deduction']){
					if($v['deduction'] == 1){
						//返还点数			
						$re = PointService::pointDetAdd($master['mid'],$company['mid'],$v['contract_id'],3,$v['point'],$desc,$master['unique']);
						if($re){
							Db::name('system_company')->where(['mid'=>$master['mid']])->setInc('point', $v['point']);
						}
					}else{
						//返还次数
						$re = PointService::frequencyDetAdd($master['mid'],$company['mid'],$v['contract_id'],$service['id'],3,1,$desc,$master['unique']);
						if($re){
							Db::name('system_member_count')->where(['member_id'=>$master['mid'],'service_id'=>$service['id']])->setInc('surplus');
							Db::name('system_member_count')->where(['member_id'=>$master['mid'],'service_id'=>$service['id']])->setInc('back');
						}
					}
				}					
			}
		}
				
	}
	
	
	
	/**
     * 导出确认信息
     * @param array $id
     * @return array
     */
    public static function export_increase_insurance($id)
    {
    	$filename = '增员确认列表';
    	$titles = array(
    		array('code','服务编号'),
            array('name','姓名'),
            array('id_number','身份证号'),
            array('mobile','电话号码'),
            array('company_name','公司名称'),                       
            array('insurance','保险类型'),
            array('hand_type','办理类型'),         
            array('wage','月发工资'),
            array('company_base','单位基数'),
            array('personal_base','个人基数'),
            array('company_prop','单位比例（%）'),
            array('person_prop','个人比例（%）'),
            array('start_date','起始日期'), 
            array('account_period','所属账期'),  
            array('area','所属区域'),  
            array('status','状态'),           
            array('confirm_date','确认日期'),
            array('csr_id','所属客服'),
            array('ssr_id','所属社保'),
            array('desc','备注信息'),
        );
    	$list = Db::name('system_increase_insurance')->where(['id'=>$id])->order('increase_id desc,id asc')->select();
		if(!empty($list) && $list){
			$companys = Db::name('system_company')->column('name','id');
			foreach($list as $k=>$v){
				$increase = Db::name('system_increase')->where(['id'=>$v['increase_id']])->find();
				$contract_insurance = Db::name('system_contract_insurance')->where(['contract_id'=>$v['contract_id'],'insurance'=>$v['insurance']])->find();
	    		$arr[$k]['code'] = $increase['code'];
	    		$arr[$k]['name'] = $increase['name'];
				$arr[$k]['id_number'] = ' '.$increase['id_number'];
				$arr[$k]['mobile'] = $increase['mobile'];
				$arr[$k]['company_name'] = $companys[$v['company_id']];
				$arr[$k]['insurance'] = get_option('insurance')[$v['insurance']];
				$arr[$k]['hand_type'] = $v['hand_type'] ? get_option('hand_type',$v['insurance'])[$v['hand_type']] : '';
				$arr[$k]['wage'] = $v['wage'];
				$arr[$k]['company_base'] = $v['company_base'];
	    		$arr[$k]['personal_base'] = $v['personal_base'];
	    		$arr[$k]['company_prop'] = $contract_insurance['company_prop'];
	    		$arr[$k]['person_prop'] = $contract_insurance['person_prop'];
				$arr[$k]['start_date'] = $v['start_date'];
				$arr[$k]['account_period'] = $v['account_period'];
				$arr[$k]['area'] = $v['area'] ? get_option('area')[$v['area']] : '';
				$arr[$k]['status'] = ['1'=>'待审核 ','2'=>'待确认','3'=>'已确认','4'=>'已反馈'][$v['status']];
				$arr[$k]['confirm_date'] = $v['confirm_date'] ? date('Y-m-d',$v['confirm_date']) : '';
				$arr[$k]['csr_id'] = $v['csr_id'] ? user_list()[$v['csr_id']] : '';
				$arr[$k]['ssr_id'] = $v['ssr_id'] ? user_list()[$v['ssr_id']] : '';
				$arr[$k]['desc'] = $v['desc'];
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }
   
    
	
	/**
     * 批量增员
     * @param array $arr
     * @return array
     */
    public static function import_increase($arr)
	{
		$suss = [];
        $err = [];
		$validate = new IncreaseValidate;
		$point = Db::name('system_service')->where(['is_special'=>2])->value('point');
		foreach($arr as $k=>$v){		
			//检查公司是否为空
			if(!$v[0]){
				$err[] = [
					'company' => $v[0],
					'name'  => $v[1],
					'idcard' => $v[2],
					'msg' => '公司名称不能为空'
				];
				continue;
			}
			//检查公司是否存在
			$company = Db::name('system_company')->where(['name'=>$v[0]])->find();
			if(!$company){
				$err[] = [
					'company' => $v[0],
					'name'  => $v[1],
					'idcard' => $v[2],
					'msg' => '公司名称不存在'
				];
				continue;
			}
			//检查公司合同状态是否正常
			$contract = Db::name('system_contract')->where(['company_id'=>$company['id'],'status'=>1,'csr_id'=>intval(session('admin_user.id'))])->find();
			if(!$contract){
				$err[] = [
					'company' => $v[0],
					'name'  => $v[1],
					'idcard' => $v[2],
					'msg' => '账号管理服务关闭，无法增员'
				];
				continue;
			}
			//检查套餐类型
			if(in_array($contract['setmeal_type'],[2,3])){
				$person_count = Db::name('system_person')->where(['company_id'=>$company['id'],'status'=>1])->count();
				if($person_count >= $contract['people']){
					$err[] = [
						'company' => $v[0],
						'name'  => $v[1],
						'idcard' => $v[2],
						'msg' => '人数套餐已达上限，无法增员'
					];
					continue;
				}
				$deduction = 0;
				$point = 0; 
			}else{
				//检查服务点数
				$deduction = self::deduction_check($company['id']);
				if(!$deduction){
					$err[] = [
						'company' => $v[0],
						'name'  => $v[1],
						'idcard' => $v[2],
						'msg' => '服务点不足，无法办理增员'
					];
					continue;
				}
			}
			//检查姓名		
			$check['name'] = $v[1];
    		if(!$validate->scene('import_name')->check($check)){
    			$err[] = [
    				'company' => $v[0],
					'name'  => $v[1],
					'idcard' => $v[2],
					'msg' => $validate->getError()
				];
				continue;
	       	}
			//检查身份证	
			$check['id_number'] = $v[2];	
    		if(!$validate->scene('import_id')->check($check)){
    			$err[] = [
    				'company' => $v[0],
					'name'  => $v[1],
					'idcard' => $v[2],
					'msg' => $validate->getError()
				];
				continue;
	       	}
			if(!is_idcard($v[2])){
				$err[] = [
					'company' => $v[0],
					'name'  => $v[1],
					'idcard' => $v[2],
					'msg' => '身份证号格式错误'
				];
				continue;
			}
			$person = Db::name('system_person')->where(['id_number'=>$v[2],'status'=>1])->count();
			if($person){
				$err[] = [
					'company' => $v[0],
					'name'  => $v[1],
					'idcard' => $v[2],
					'msg' => '该员工已在职'
				];
				continue;
			}
			$count = Db::name('system_increase')->where(['id_number'=>$v[2],'is_deleted'=>0,'status'=>1])->count();
			if($count){
				$err[] = [
					'company' => $v[0],
					'name'  => $v[1],
					'idcard' => $v[2],
					'msg' => '该员工正在办理增员'
				];
				continue;
			}
			//检查电话号码
			$check['mobile'] = $v[3];	
			if(!$validate->scene('import_mobile')->check($check)){
    			$err[] = [
    				'company' => $v[0],
					'name'  => $v[1],
					'idcard' => $v[2],
					'msg' => $validate->getError()
				];
				continue;
	       	}
			//检查服务账期
			if(!$v[4]){
    			$err[] = [
    				'company' => $v[0],
					'name'  => $v[1],
					'idcard' => $v[2],
					'msg' => '服务账期不能为空'
				];
				continue;
	       	}else{
	       		$timestamp = strlen($v[4]) == 5 ? strtotime(excel_time_eval($v[4])) : strtotime($v[4]); 
	       		if(!is_timestamp($timestamp)){
	       			$err[] = [
	    				'company' => $v[0],
						'name'  => $v[1],
						'idcard' => $v[2],
						'msg' => '服务账期格式错误'
					];
					continue;
	       		}
	       		$start_date = strlen($v[4]) == 5 ? date('Y-m',strtotime(excel_time_eval($v[4]))) : date('Y-m',strtotime($v[4]));
	       	}
			//检查月发工资
			if(!$v[5]){
				$err[] = [
					'company' => $v[0],
					'name'  => $v[1],
					'idcard' => $v[2],
					'msg' => '月发工资不能为空'
				];
				continue;
			}
			if(!is_numeric($v[5]) || $v[5] <= 0){
				$err[] = [
					'company' => $v[0],
					'name'  => $v[1],
					'idcard' => $v[2],
					'msg' => '月发工资格式错误'
				];
				continue;
			}
			//检查托管档案
			if(!$v[6] || $v[6] == '否'){
				$archives = 0;
			}else{
				$archives = 1;
			}
			//检查银行卡号
//			$check['bank_account'] = $v[7];
//			if(!$validate->scene('import_bank_account')->check($check)){
//  			$err[] = [
//  				'company' => $v[0],
//					'name'  => $v[1],
//					'idcard' => $v[2],
//					'msg' => $validate->getError()
//				];
//				continue;
//	       	}
			//检查开户银行
//			$check['bank'] = $v[8];
//			if(!$validate->scene('import_bank')->check($check)){
//  			$err[] = [
//  				'company' => $v[0],
//					'name'  => $v[1],
//					'idcard' => $v[2],
//					'msg' => $validate->getError()
//				];
//				continue;
//	       	}
			//检查银行行号
//			$check['bank_number'] = $v[9];
//			if(!$validate->scene('import_bank_number')->check($check)){
//  			$err[] = [
//  				'company' => $v[0],
//					'name'  => $v[1],
//					'idcard' => $v[2],
//					'msg' => $validate->getError()
//				];
//				continue;
//	       	}
			//检查养老
			if($v[7] != '否' && $v[8]){
				$timestamp = strlen($v[8]) == 5 ? strtotime(excel_time_eval($v[8])) : strtotime($v[8]); 
	       		if(!is_timestamp($timestamp)){
	       			$err[] = [
	    				'company' => $v[0],
						'name'  => $v[1],
						'idcard' => $v[2],
						'msg' => '养老起始日期格式错误'
					];
					continue;
	       		}
	       		if(!after_accounting($start_date,date('Y-m-d',$timestamp))){
	       			$err[] = [
	    				'company' => $v[0],
						'name'  => $v[1],
						'idcard' => $v[2],
						'msg' => '养老起始日期超期'
					];
					continue;
	       		}
			}
			if($v[7] != '否' && $v[9]){
				if(!is_numeric($v[9]) || $v[9] <= 0){					
					$err[] = [
						'company' => $v[0],
						'name'  => $v[1],
						'idcard' => $v[2],
						'msg' => '养老社保工资格式错误'
					];
					continue;
				}
			}
			//检查工伤
			if($v[10] != '否' && $v[11]){
				$timestamp = strlen($v[11]) == 5 ? strtotime(excel_time_eval($v[11])) : strtotime($v[11]); 
	       		if(!is_timestamp($timestamp)){
	       			$err[] = [
	    				'company' => $v[0],
						'name'  => $v[1],
						'idcard' => $v[2],
						'msg' => '工伤起始日期格式错误'
					];
					continue;
	       		}
	       		if(!after_accounting($start_date,date('Y-m-d',$timestamp))){
	       			$err[] = [
	    				'company' => $v[0],
						'name'  => $v[1],
						'idcard' => $v[2],
						'msg' => '工伤起始日期超期'
					];
					continue;
	       		}
			}
			if($v[10] != '否' && $v[12]){
				if(!is_numeric($v[12]) || $v[12] <= 0){					
					$err[] = [
						'company' => $v[0],
						'name'  => $v[1],
						'idcard' => $v[2],
						'msg' => '工伤社保工资格式错误'
					];
					continue;
				}
			}
			//检查失业
			if($v[13] != '否' && $v[14]){
				$timestamp = strlen($v[14]) == 5 ? strtotime(excel_time_eval($v[14])) : strtotime($v[14]); 
	       		if(!is_timestamp($timestamp)){
	       			$err[] = [
	    				'company' => $v[0],
						'name'  => $v[1],
						'idcard' => $v[2],
						'msg' => '失业起始日期格式错误'
					];
					continue;
	       		}
	       		if(!after_accounting($start_date,date('Y-m-d',$timestamp))){
	       			$err[] = [
	    				'company' => $v[0],
						'name'  => $v[1],
						'idcard' => $v[2],
						'msg' => '失业起始日期超期'
					];
					continue;
	       		}
			}
			if($v[13] != '否' && $v[15]){
				if(!is_numeric($v[15]) || $v[15] <= 0){					
					$err[] = [
						'company' => $v[0],
						'name'  => $v[1],
						'idcard' => $v[2],
						'msg' => '失业社保工资格式错误'
					];
					continue;
				}
			}
			//检查医疗
			if($v[16] != '否' && $v[17]){
				$timestamp = strlen($v[17]) == 5 ? strtotime(excel_time_eval($v[17])) : strtotime($v[17]); 
	       		if(!is_timestamp($timestamp)){
	       			$err[] = [
	    				'company' => $v[0],
						'name'  => $v[1],
						'idcard' => $v[2],
						'msg' => '医疗起始日期格式错误'
					];
					continue;
	       		}
	       		if(!after_accounting($start_date,date('Y-m-d',$timestamp))){
	       			$err[] = [
	    				'company' => $v[0],
						'name'  => $v[1],
						'idcard' => $v[2],
						'msg' => '医疗起始日期超期'
					];
					continue;
	       		}
			}
			if($v[16] != '否' && $v[18]){
				if(!is_numeric($v[18]) || $v[18] <= 0){					
					$err[] = [
						'company' => $v[0],
						'name'  => $v[1],
						'idcard' => $v[2],
						'msg' => '医疗社保工资格式错误'
					];
					continue;
				}
			}
			//检查公积金
			if($v[19] != '否' && $v[20]){
				$timestamp = strlen($v[20]) == 5 ? strtotime(excel_time_eval($v[20])) : strtotime($v[20]); 
	       		if(!is_timestamp($timestamp)){
	       			$err[] = [
	    				'company' => $v[0],
						'name'  => $v[1],
						'idcard' => $v[2],
						'msg' => '公积金起始日期格式错误'
					];
					continue;
	       		}
	       		if(!after_accounting($start_date,date('Y-m-d',$timestamp))){
	       			$err[] = [
	    				'company' => $v[0],
						'name'  => $v[1],
						'idcard' => $v[2],
						'msg' => '公积金起始日期超期'
					];
					continue;
	       		}
			}
			if($v[19] != '否' && $v[21]){
				if(!is_numeric($v[21]) || $v[21] <= 0){					
					$err[] = [
						'company' => $v[0],
						'name'  => $v[1],
						'idcard' => $v[2],
						'msg' => '公积金工资格式错误'
					];
					continue;
				}
			}
			$code = number_rule_max(7);
			$inc = [
				'code' => $code,
				'name' => $v[1],
				'id_number' => $v[2],
				'mobile' => $v[3],
				'company_id' => $company['id'],
				'contract_id' => $contract['id'],
				'start_date' => $start_date,
				'wage' => $v[5],
				'archives' => $archives,
				'csr_id' => $contract['csr_id'],
				'source' => 1,
				'deduction' => $deduction,
				'point' => $point
			];
			$inc_id = Db::name('system_increase')->insertGetId($inc); 
			if($inc_id){
				save_number_rule_max(7);
			}
			//扣除服务点数或次数
			self::deduction($inc_id);
			$insurances = Db::name('system_contract_insurance')->where(['contract_id'=>$contract['id'],'status'=>1])->column('insurance');
			$areas = Db::name('system_contract_insurance')->where(['contract_id'=>$contract['id'],'status'=>1])->column('area','insurance');
	    	if($v[7] != '否' && in_array(1, $insurances)){
	    		$wage = $v[9] ? $v[9] : $v[5];
				if($v[8]){
					$sdate = strlen($v[8]) == 5 ? date('Y-m-d',strtotime(excel_time_eval($v[8]))) : date('Y-m-d',strtotime($v[8])); 
				}else{
					$sdate = $start_date.'-01';
				}
	    		$data = [
	    			'increase_id' => $inc_id,
	    			'company_id' => $company['id'],
	    			'contract_id' => $contract['id'],
	    			'insurance' => 1,
	    			'area' => $areas[1],
	    			'status' => 1,
	    			'wage' => $wage,
	    			'company_base' => self::oneBase(1,$wage)['company'],
	    			'personal_base' => self::oneBase(1,$wage)['personal'],
	    			'start_date' => $sdate,
	    			'csr_id' => $contract['csr_id']
	    		]; 
	    	}else{
	    		$data = [
	    			'increase_id' => $inc_id,
	    			'company_id' => $company['id'],
	    			'contract_id' => $contract['id'],
	    			'insurance' => 1,
	    			'area' => 0,
	    			'status' => 0,
	    			'wage' => 0,
	    			'company_base' => 0,
	    			'personal_base' => 0,
	    			'start_date' => '',
	    			'csr_id' => $contract['csr_id']
	    		];				
	    	}
			Db::name('system_increase_insurance')->insert($data);
	    	if($v[10] != '否' && in_array(2, $insurances)){
	    		$wage = $v[12] ? $v[12] : $v[5];
				if($v[11]){
					$sdate = strlen($v[11]) == 5 ? date('Y-m-d',strtotime(excel_time_eval($v[11]))) : date('Y-m-d',strtotime($v[11])); 
				}else{
					$sdate = $start_date.'-01';
				}
	    		$data = [
	    			'increase_id' => $inc_id,
	    			'company_id' => $company['id'],
	    			'contract_id' => $contract['id'],
	    			'insurance' => 2,
	    			'area' => $areas[2],
	    			'status' => 1,
	    			'wage' => $wage,
	    			'company_base' => self::oneBase(2,$wage)['company'],
	    			'personal_base' => self::oneBase(2,$wage)['personal'],
	    			'start_date' => $sdate,
	    			'csr_id' => $contract['csr_id']
	    		];				 
	    	}else{
	    		$data = [
	    			'increase_id' => $inc_id,
	    			'company_id' => $company['id'],
	    			'contract_id' => $contract['id'],
	    			'insurance' => 2,
	    			'area' => 0,
	    			'status' => 0,
	    			'wage' => 0,
	    			'company_base' => 0,
	    			'personal_base' => 0,
	    			'start_date' => '',
	    			'csr_id' => $contract['csr_id']
	    		];				
	    	}
			Db::name('system_increase_insurance')->insert($data);	
			if($v[13] != '否' && in_array(3, $insurances)){
	    		$wage = $v[15] ? $v[15] : $v[5];
				if($v[14]){
					$sdate = strlen($v[14]) == 5 ? date('Y-m-d',strtotime(excel_time_eval($v[14]))) : date('Y-m-d',strtotime($v[14])); 
				}else{
					$sdate = $start_date.'-01'; 
				}
	    		$data = [
	    			'increase_id' => $inc_id,
	    			'company_id' => $company['id'],
	    			'contract_id' => $contract['id'],
	    			'insurance' => 3,
	    			'area' => $areas[3],
	    			'status' => 1,
	    			'wage' => $wage,
	    			'company_base' => self::oneBase(3,$wage)['company'],
	    			'personal_base' => self::oneBase(3,$wage)['personal'],
	    			'start_date' => $sdate,
	    			'csr_id' => $contract['csr_id']
	    		];
	    	}else{
	    		$data = [
	    			'increase_id' => $inc_id,
	    			'company_id' => $company['id'],
	    			'contract_id' => $contract['id'],
	    			'insurance' => 3,
	    			'area' => 0,
	    			'status' => 0,
	    			'wage' => 0,
	    			'company_base' => 0,
	    			'personal_base' => 0,
	    			'start_date' => '',
	    			'csr_id' => $contract['csr_id']
	    		];				
	    	}
			Db::name('system_increase_insurance')->insert($data);	
	    	if($v[16] != '否' && in_array(4, $insurances)){
	    		$wage = $v[18] ? $v[18] : $v[5];
				if($v[17]){
					$sdate = strlen($v[17]) == 5 ? date('Y-m-d',strtotime(excel_time_eval($v[17]))) : date('Y-m-d',strtotime($v[17])); 				
				}else{
					$sdate = $start_date.'-01'; 
				}	
	    		$data = [
	    			'increase_id' => $inc_id,
	    			'company_id' => $company['id'],
	    			'contract_id' => $contract['id'],
	    			'insurance' => 4,
	    			'area' => $areas[4],
	    			'status' => 1,
	    			'wage' => $wage,
	    			'company_base' => self::oneBase(4,$wage)['company'],
	    			'personal_base' => self::oneBase(4,$wage)['personal'],
	    			'start_date' => $sdate,
	    			'csr_id' => $contract['csr_id']
	    		]; 
	    	}else{
	    		$data = [
	    			'increase_id' => $inc_id,
	    			'company_id' => $company['id'],
	    			'contract_id' => $contract['id'],
	    			'insurance' => 4,
	    			'area' => 0,
	    			'status' => 0,
	    			'wage' => 0,
	    			'company_base' => 0,
	    			'personal_base' => 0,
	    			'start_date' => '',
	    			'csr_id' => $contract['csr_id']
	    		];				
	    	}
			Db::name('system_increase_insurance')->insert($data);		
			if($v[19] != '否' && in_array(5, $insurances)){
	    		$wage = $v[21] ? $v[21] : $v[5];
				if($v[20]){
					$sdate = strlen($v[20]) == 5 ? date('Y-m-d',strtotime(excel_time_eval($v[20]))) : date('Y-m-d',strtotime($v[20])); 
				}else{
					$sdate = $start_date.'-01'; 
				}
	    		$data = [
	    			'increase_id' => $inc_id,
	    			'company_id' => $company['id'],
	    			'contract_id' => $contract['id'],
	    			'insurance' => 5,
	    			'area' => $areas[5],
	    			'status' => 1,
	    			'wage' => $wage,
	    			'company_base' => self::oneBase(5,$wage)['company'],
	    			'personal_base' => self::oneBase(5,$wage)['personal'],
	    			'start_date' => $sdate,
	    			'csr_id' => $contract['csr_id']
	    		]; 
	    	}else{
	    		$data = [
	    			'increase_id' => $inc_id,
	    			'company_id' => $company['id'],
	    			'contract_id' => $contract['id'],
	    			'insurance' => 5,
	    			'area' => 0,
	    			'status' => 0,
	    			'wage' => 0,
	    			'company_base' => 0,
	    			'personal_base' => 0,
	    			'start_date' => '',
	    			'csr_id' => $contract['csr_id']
	    		];				
	    	}
			Db::name('system_increase_insurance')->insert($data);	
			operatelog($inc_id,'SystemIncrease','进行了新增');	
			$suss[] = [
				'company' => $v[0],
				'name'  => $v[1],
				'idcard' => $v[2],
				'msg' => '增员成功'
			];					    
		}
		return ['suss'=>$suss,'err'=>$err];
	}



}
